<?php

    /* 分类删除 */
    function getcateDel($link,$pid,$a=''){

            $sql = "select * from `cate` where `pid` = $pid";
            $result = mysqli_query($link,$sql);

            if ($result->num_rows > 0) {

                while ($arr = mysqli_fetch_assoc($result)) {

                    $a = getcateDel($link,$arr['id'],$a);
                }
            }
            $sql = "delete from `cate` where `id` = $pid";
            mysqli_query($link,$sql);


    }

    /* 分类列表 */
    function getcate($link,$pid=0,$cate=array(),$k=0){

            $str = '';
            for ($i=0; $i < $k ; $i++) {
                $str .= '●';
            }

            $sql = "select * from `cate` where `pid` = $pid";
            $result = mysqli_query($link,$sql);

            while ($arr = mysqli_fetch_assoc($result)) {

                $arr['name'] = $str.$arr['name'];
                $cate[] = $arr;
                $pid = $arr['id'];

                $cate = getcate($link,$pid,$cate,$k+1);
            }

            return $cate;
    }

    /* 分类编辑 */
    function editcate($link,$data,$cate=array()){

            $pid = $data['pid'];//echo $pid;die;
            //print_r($data);die;
            //echo $data['pid'];die;
            $sql = "select * from `cate` where `pid` = $pid";
            $result = mysqli_query($link,$sql);

            while ($arr = mysqli_fetch_assoc($result)) {

                $cate[$data['id']][] = $arr;
                $id = $arr['pid'];

            }

            if($id != 0){
                $sql = "select * from `cate` where `id`='$id'";
                $result = mysqli_query($link,$sql);
                $data = mysqli_fetch_assoc($result);
                $cate = editcate($link,$data,$cate);
            }

            return $cate;
    }

?>